package Test.D6;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;



import Test.D5.D5RandmName;

public class Test6_2 {
    public static void main(String[] args) {
        Test6_2 myclass = new Test6_2();
        myclass.TestAddFun();
        System.out.println("\n所有学生成绩(根据成绩高到低排序):\n");
        myclass.showScores(new ScoresComparator());
        
        System.out.println("\n所有学生成绩(根据学号低到高排序):\n");
        myclass.showScores(new IdComparator1());

        System.out.println("\n所有学生成绩(根据学号高到低排序):\n");
        myclass.showScores(new IdComparator2());

        System.out.println("\n学生 180231 成绩:\n");
        myclass.showToId(180231);
    }

    /**
     * 测试添加40名学生成绩
     */
    public void TestAddFun()
    {
        mDatas = new ArrayList<Studant>();
        for(int i=180201;i<180241;i++)
        {
            mDatas.add(new Studant(i));
        }
    }

    private ArrayList<Studant> mDatas;

    

    //显示所有学生成绩
    public void showScores(Comparator comparator)
    {
        Collections.sort(mDatas, comparator);

        Iterator<Studant> it = mDatas.iterator();
        while(it.hasNext()){
            System.out.println(it.next());
        }

    }

    public void showToId(int id)
    {
        Iterator<Studant> it = mDatas.iterator();
        while(it.hasNext()){
            Studant studant = it.next();
            if(studant.getId()==id)
            {
                System.out.println(studant);
                return;
            }
            
        }
    }

}

class Studant{
    //学号 姓名
    private int mid;
    private String mname;
    //分数
    private int mscores;

    public Studant(int id)
    {
        mid = id;
        getRound();
    }

    //随机获得姓名和成绩
    private void getRound()
    {
        mname = D5RandmName.GetName();
        mscores = 51+D5RandmName.getNum(50);
    }
    //获得学号
    public int getId()
    {
        return mid;
    }
    //获得成绩
    public int getScores()
    {
        return mscores;
    }
    
    //获得名字
    public String getName()
    {
        return mname;
    }

    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return "学号:"+mid+" 姓名:"+mname+" 成绩:"+mscores;
    }


}

class ScoresComparator implements Comparator
{

    @Override
    public int compare(Object arg0, Object arg1) {
        // TODO Auto-generated method stub  
        Studant obj1 = (Studant)arg0;
        Studant obj2 = (Studant)arg1;
        if(obj1.getScores()<obj2.getScores())
            return 1;
        else if(obj1.getScores()==obj2.getScores())
        {
            return obj1.getId()-obj2.getId();
        }
        return -1;
        
    }
    
}

class IdComparator1 implements Comparator
{

    @Override
    public int compare(Object arg0, Object arg1) {
        // TODO Auto-generated method stub  
        Studant obj1 = (Studant)arg0;
        Studant obj2 = (Studant)arg1;
        return obj1.getId()-obj2.getId();
    }
    
}

class IdComparator2 implements Comparator
{

    @Override
    public int compare(Object arg0, Object arg1) {
        // TODO Auto-generated method stub  
        Studant obj1 = (Studant)arg0;
        Studant obj2 = (Studant)arg1;
        return obj2.getId()-obj1.getId();
    }
    
}